/**
 * 
 */
package com.fox.webapp.service.impl;

import java.util.List;
import java.util.Locale;

import javax.mail.MessagingException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.fox.webapp.domain.LoginLog;
import com.fox.webapp.domain.Person;
import com.fox.webapp.persistent.LoginLogRepository;
import com.fox.webapp.persistent.PersonRepository;
import com.fox.webapp.service.IAsyncActionService;
import com.fox.webapp.service.IEmailService;

/**
 * @author yockii
 *
 */
@Service
public class AsyncActionService implements IAsyncActionService {

	private static final Logger logger = LoggerFactory.getLogger(AsyncActionService.class);
	@Autowired
	private IEmailService emailService;
	@Autowired
	private PersonRepository personRepository;
	@Autowired
	private LoginLogRepository loginLogRepository;
	
	@Override
	@Async
	public void sendWelcomeEmailAsync(Person person, Locale locale) {
		try {
			emailService.welcomEmail(person, locale);
		} catch (MessagingException e) {
			e.printStackTrace();
			try {
				emailService.errorMessageEmail("欢迎邮件"+person.getUsername()+"\n"+e.getMessage());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}


	@Override
	@Async
	public void sendErrorMessageAsync(String msg) {
		try {
			emailService.errorMessageEmail(msg);
		} catch (MessagingException e) {
			e.printStackTrace();
			try {
				emailService.errorMessageEmail(e.getMessage());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}


	@Override
	@Async
	public void sendVerifyEmail(Person p) {
		try {
			emailService.verifyEmail(p);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				emailService.errorMessageEmail("验证Email:"+p.getEmail() + "\n"+e.getMessage());
			} catch (MessagingException e1) {
				e1.printStackTrace();
			}
		}
	}


	@Override
	@Async
	public void sendResetPasswordEmail(Person p) {
		try{
			emailService.sendResetPwEmail(p);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				emailService.errorMessageEmail("重置密码:"+p.getUsername() + "\n" + e.getMessage());
			} catch (MessagingException e1) {
				e1.printStackTrace();
			}
		}
	}


	@Override
	@Async
	public void sendModifyEmailEmail(Person p) {
		try{
			emailService.sendModifyEmailEmail(p);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				emailService.errorMessageEmail("重置Email:"+p.getUsername() + "\n" + e.getMessage());
			} catch (MessagingException e1) {
				e1.printStackTrace();
			}
		}
	}


	@Override
	@Async
	public void lookingForIp(Person p) {
		if(p.getIsIpWarning() != null && p.getIsIpWarning()){
			return;
		}
		List<Person> pl = personRepository.findByRegisterIp(p.getLastLoginIp());
		if(pl != null && pl.size() > 0){
			for (Person person : pl) {
				if(p.getId().toString().equals(person.getReferenceId()) || p.getReferenceId().equals(person.getId().toString())){
					p.setIsIpWarning(true);
					person.setIsIpWarning(true);
					personRepository.updatePersonIpWarning(p, person);
					return;
				}
			}
		}
		List<LoginLog> lll = loginLogRepository.findDistinctLoginLogByIpAddr(p.getLastLoginIp());
		if(lll != null && lll.size() > 0){
			for (LoginLog loginLog : lll) {
				if(loginLog.getUsername().equalsIgnoreCase(p.getUsername())){
					continue;
				}
				Person person = personRepository.findByUsername(loginLog.getUsername());
				if(person != null && (person.getReferenceId().equals(p.getId().toString()) || p.getReferenceId().equals(person.getId().toString()))){
					p.setIsIpWarning(true);
					person.setIsIpWarning(true);
					personRepository.updatePersonIpWarning(p, person);
					return;
				}
			}
		}
	}
	
}
